package com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dl7.recycler.adapter.BaseMultiItemQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.supertextview.SuperTextView;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.WrapBookInfo;

import java.util.List;

/**
 * 完本适配器
 * Created by wcy on 18/5/22.
 */
public class FinishedAdapter extends BaseMultiItemQuickAdapter<WrapBookInfo> {
    public static final int TYPE_ONE = 0;
    public static final int TYPE_TWO = 1;
    private int mWidth, mHeight;
    private int mType;
    public FinishedAdapter(Context context, List<WrapBookInfo> data) {
        super(context, data);
        mWidth = DisplayUtils.dip2px(context, 89);
        mHeight = DisplayUtils.dip2px(context, 112);
    }

    public void setType(int type) {
        mType = type;
    }

    public FinishedAdapter(Context context) {
        super(context);
    }

    @Override
    protected int getDefaultLayoutId() {
        return R.layout.item_default_layout;
    }

    @Override
    protected void attachItemType() {
        addItemType(WrapBookInfo.TYPE_ONE, R.layout.item_finished_fixed_layout);
        addItemType(WrapBookInfo.TYPE_TWO, R.layout.item_book_layout);

    }

    @Override
    protected void convert(BaseViewHolder holder, WrapBookInfo item, int position) {
        position = position - getHeaderViewsCount();
        switch (item.getItemType()) {
            case WrapBookInfo.TYPE_ONE:
                handleStyle1(holder, item, position);
                break;
            case WrapBookInfo.TYPE_TWO:
                handleStyle2(holder, item, position);
                break;
            default:
                break;
        }
    }

    private void handleStyle1(BaseViewHolder holder, WrapBookInfo item, int position) {
        LinearLayout linearLayout = holder.getView(R.id.root_container);
        for (int i = 0; i < linearLayout.getChildCount(); i++) {
            FrameLayout layout = (FrameLayout) linearLayout.getChildAt(i);
            layout.setVisibility(View.GONE);
            final int pos = i;
            layout.setOnClickListener(v -> {
                ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", item.items.get(pos).book_id);
            });
        }
        if (mType == TYPE_TWO) {
            holder.setVisible(R.id.divider, true);
        }
        if (!Utils.isEmptyList(item.items)) {
            if (item.items.size() > linearLayout.getChildCount()) {
                item.items = item.items.subList(0, linearLayout.getChildCount());
            }
            for (int i = 0; i < item.items.size(); i++) {
                linearLayout.getChildAt(i).setVisibility(View.VISIBLE);
                FrameLayout frameLayout = (FrameLayout) linearLayout.getChildAt(i);
                LinearLayout layout = (LinearLayout) frameLayout.getChildAt(0);
                ImageView imageView = (ImageView) layout.getChildAt(0);
                TextView tv1 = (TextView) layout.getChildAt(1);
                SuperTextView tv2 = (SuperTextView) layout.getChildAt(2);
                GlideImageLoader.load(item.items.get(i).cover_url, imageView);
                tv1.setText(item.items.get(i).book_title);
                tv2.setText(BookUtils.getBookLabel(item.items.get(i)));
            }
         }
    }

    private void handleStyle2(BaseViewHolder holder, WrapBookInfo item, final int position) {
        holder.setText(R.id.book_title, item.item.book_title).setText(R.id.book_desc, item.item.book_intro)
                .setText(R.id.author, item.item.author_name).setText(R.id.book_label, BookUtils.getBookLabel(item.item))
                .setText(R.id.book_status, BookUtils.getBookStatus(item.item));
        ImageView imageView = holder.getView(R.id.cover);
        holder.setVisible(R.id.bottom_container, true);
        ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) imageView.getLayoutParams();
        params.width = mWidth;
        params.height = mHeight;
        imageView.setLayoutParams(params);
        GlideImageLoader.load(item.item.cover_url, imageView);
        BookUtils.setStatusBg(holder.getView(R.id.book_status), item.item);
        holder.getConvertView().setOnClickListener(v -> {
            ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", item.item.book_id);
        });
    }
}
